<template>
  <div>
    <FormPanel
      ref="from"
      :list="formList"
      @formChange="changeHandle"
      :isSubmitBtn="false"
      :collapse="false"
      labelWidth="110"
    />
    <div style="height:47px;"></div>
    <BaseDialog
      :visible.sync="isSelectCar"
      :title="'车辆查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <vinSelect :ownerCode="dealerCode" @close="carCloseHandler" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="issearch"
      :title="'选择经销商'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeachers @close="ownercloseHandler" />
    </BaseDialog>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">提 交</multiuse-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 3)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import { updatePlateApply, getcarall } from '@/api/omSale/PurchaseReturn/ReturnAudit/index.js';
import { dictionary } from '@/utils/dictMixin';
import ownerSeachers from './ownerSeachers';
import vinSelect from './vinSelect';
import moment from 'moment';
import Cookie from 'js-cookie';
export default {
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      issearch: false,
      isSelectCar: false,
      carData: {},
      formData: {},
      dealerCode: '',
      userName: Cookie.get('username'),
      userId: Cookie.get('userId')
    };
  },
  mixins: [dictionary],
  components: {
    vinSelect,
    ownerSeachers
  },
  created() {
    this.formList.find(x => x.fieldName == 'applyPersonName').initialValue = this.userName;
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '退货单号',
          fieldName: 'returnNo',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          placeholder: '请输入',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: val => {
            this.dealerCode = val;
          }
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'applyDate',
          placeholder: '请输入',
          dateType: 'exactdate',
          disabled: true,
          initialValue: moment().format('YYYY-MM-DD')
        },
        {
          type: 'SELECT',
          label: '退车原因',
          fieldName: 'returnReason',
          placeholder: '请输入',
          itemList: this.createDictList('6056'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '退货人',
          fieldName: 'applyPersonName',
          placeholder: '请输入',
          disabled: true
        },

        {
          type: 'INPUT',
          label: '店内联系人',
          fieldName: 'linkMan',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          // readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: val => {
            this.vinChange(val);
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        },

        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'doNo',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '联系方式',
          fieldName: 'linkPhone',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原价格',
          fieldName: 'afterDiscountRebateAmount',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原发票号',
          fieldName: 'goldenInvoiceNo',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原税率',
          fieldName: 'rate',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '退车金额',
          fieldName: 'returnAmount',
          disabled: true,
          placeholder: '请输入',
          max: 1000000000
        },
        {
          type: 'SELECT',
          label: '店端车辆状态',
          fieldName: 'vehicleStatus',
          placeholder: '请输入',
          disabled: true,
          itemList: this.createDictList('6040')
        },
        {
          type: 'TEXT_AREA',
          label: '退车原因说明',
          fieldName: 'returnReasonRemark',
          placeholder: '请输入',
          selfCols: 3
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件上传',
          fieldName: 'pos',
          placeholder: '请输入',
          initialValue: [],
          offsetRightCols: 2,
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 1000,
            tipText: '最多上传1000个附件'
          }
        }
      ];
    },
    async vinChange(val) {
      if (val) {
        let res = await getcarall({
          vin: val,
          isLock: 10041002,
          vehicleStatus: 60401003,
          current: 1,
          currentPage: 1,
          pageNum: 1,
          size: 20,
          pageSize: 20
        });
        debugger;
        if (res.resultCode == 200) {
          this.carCloseHandler(res.data.records[0]);
        }
      }
    },
    seacherFun(type) {
      if (type == 1) {
        this.issearch = true;
      } else {
        debugger;
        this.isSelectCar = true;
        // if (this.dealerCode) {
          
        // } else {
        //   this.$notify({
        //     title: '警告',
        //     message: '请先选择经销商',
        //     type: 'warning'
        //   });
        //   return;
        // }
      }
    },
    ownercloseHandler(val) {
      if (val) {
        console.log(val);
        this.formList.map(x => {
          
          if (x.fieldName == 'dealerName') {
            x.initialValue = val.dealerName;
            this.dealerCode = val.dealerCode;
          }
          if (x.fieldName == 'linkMan') {
            x.initialValue = val.emergencyContract;
          }
          if (x.fieldName == 'linkPhone') {
            x.initialValue = val.emergencyContractPhone;
          }
          if (x.fieldName == 'vin') {
            x.initialValue = '';
          }
          if (x.fieldName == 'doNo') {
            x.initialValue = '';
          }
          if (x.fieldName == 'productName') {
            x.initialValue = '';
          }
          if (x.fieldName == 'afterDiscountRebateAmount') {
            x.initialValue = '';
          }
          if (x.fieldName == 'goldenInvoiceNo') {
            x.initialValue = '';
          }
          if (x.fieldName == 'rate') {
            x.initialValue = '';
          }
          if (x.fieldName == 'returnAmount') {
            x.initialValue = '';
          }
        });
      }
      this.issearch = false;
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else if (type == 2) {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          json.reportPersonId = this.userId;
          if (json.pos && json.pos.length > 0) {
            json.pos.map(x => {
              x.fileName = x.name;
              x.fileAddress = x.url;
            });
          }
          json.returnStatus = 60551002;
          json.dealerCode = this.dealerCode;
          let res = await updatePlateApply(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      } else {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          json.reportPersonId = this.userId;
          json.dealerCode = this.dealerCode;
          if (json.pos && json.pos.length > 0) {
            json.pos.map(x => {
              x.fileName = x.name;
              x.fileAddress = x.url;
            });
          }
          json.returnStatus = 60551001;
          let res = await updatePlateApply(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    },
    carCloseHandler(val) {
      debugger
      if (val) {
        this.formList.map(x => {
          if (x.fieldName == 'vin') {
            x.initialValue = val.vin;
          }
          if (x.fieldName == 'dealerCode') {
            x.initialValue = val.dealerCode;
          }
          if (x.fieldName == 'dealerName') {
            debugger
            x.initialValue = val.dealerName;
          }
          if (x.fieldName == 'doNo') {
            x.initialValue = val.doNo;
          }
          if (x.fieldName == 'productName') {
            x.initialValue = val.productName;
          }
          if (x.fieldName == 'afterDiscountRebateAmount') {
            x.initialValue = val.afterDiscountRebateAmount;
          }
          if (x.fieldName == 'goldenInvoiceNo') {
            x.initialValue = val.goldenInvoiceNo;
          }
          if (x.fieldName == 'rate') {
            x.initialValue = val.rate;
          }
          if (x.fieldName == 'returnAmount') {
            x.initialValue = val.afterDiscountRebateAmount;
          }
          if (x.fieldName == 'vehicleStatus') {
            x.initialValue = val.vehicleStatus;
          }
          if(x.fieldName=='dealerName'){
            x.initialValue=val.dealerName;
            this.dealerCode=val.dealerCode
          }
          if (x.fieldName == 'linkMan') {
            x.initialValue = val.linkMan;
          }
          if (x.fieldName == 'linkPhone') {
            x.initialValue = val.linkPhone;
          }
        });
        this.carData = val;
      }
      this.isSelectCar = false;
    }
  }
};
</script>
